<?php
	//命名空间
	namespace Admin\Controller;
	use Think\Controller;

	//定义控制器
	class UserController extends Controller{
		
		//用户登录
		public function login(){
            if(cookie('id')){
                $this->display('Index/index');
            }else{
                if(IS_POST){
                    // //接收数据
                    $code = I('code');
                    $username = I('username');
                    $password = I('password');
                    //先验证验证码
                    $verify = new \Think\Verify();
                    if($verify->check($code)){
                        //帐号密码的验证
                        $res = M('user')->where(array('username'=>$username))->find();
                        if (!$res['username']) {
                            $this->error('用户名错误');
                        }
                        if($res['password'] != md5('gp08'.$password)){
                            $this->error('密码错误');
                        }else{
                            //存数据session(name,value)   session('id',$res['id'])
                            //读数据session(name)   session('id')
                            //删除单条session(name,null)       session('id',null)
                            //删除所有session(null)
                            cookie('id',$res['id']);
                            cookie('username',$username);
                            session('id',$res['id']);
                            session('username',$username);
                            $this->display('Index/index');
                        }
                    }else{
                        $this->error('验证码错误');
                    }
                }else{
                    $this->display();
                }
            }


		}

        //用户退出
        public function logout(){
            session(null);
            $this->success('安全退出成功',U('User/login'));
        }

        //验证码
        public function verify(){
            //配置
            $config = array(
                'length'    =>  4,
                'useImgBg'  =>  true,
            );
            //实例化验证码类
            $verify = new \Think\Verify($config);
            //生成验证码
            $verify->entry();
        }

	}

?>